package com.imedway.ihe.dtz.xds.repository;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRetrieveDocumentSetResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetRequestType;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetResponseType;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetRequestType.DocumentRequest;
import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocument;

import com.imedway.ihe.dtz.xds.dao.DataDao;
import com.imedway.ihe.dtz.xds.dto.XdsDto;
import com.imedway.ihe.dtz.xds.entity.IheXdsFileInfo;
import com.imedway.ihe.dtz.xds.util.DhccFileUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Iti43Repository_backup {
	
	public RetrieveDocumentSetResponseType run(RetrieveDocumentSetRequestType body) {
		 List<DocumentRequest> documentRequestList=body.getDocumentRequest();
		    
			EbXMLRetrieveDocumentSetResponse30 ebXMLRetrieveDocumentSetResponse30= 	new EbXMLRetrieveDocumentSetResponse30(new RetrieveDocumentSetResponseType());

			List<RetrievedDocument> documentResponseList = new ArrayList<RetrievedDocument>();

		    for(DocumentRequest documentRequest:documentRequestList) {
				try {
					List<IheXdsFileInfo> iheXdsInfoFileList=find(documentRequest.getDocumentUniqueId());
					for(IheXdsFileInfo iheXdsFileInfo:iheXdsInfoFileList) {
						documentResponseList.add(getDocument(iheXdsFileInfo));
					}
				} catch (SQLException e) {
					log.info(e.getMessage(), e);
				}

		    }			
		    
			ebXMLRetrieveDocumentSetResponse30.setDocuments(documentResponseList);
			
	        return ebXMLRetrieveDocumentSetResponse30.getInternal();
	}
	
	public RetrievedDocument getDocument(IheXdsFileInfo iheXdsFileInfo) {
		RetrievedDocument retrievedDocument1 = new RetrievedDocument();
		retrievedDocument1.setMimeType(iheXdsFileInfo.getMimeType());
		retrievedDocument1.setNewDocumentUniqueId(iheXdsFileInfo.getUniqueId());
		retrievedDocument1.setNewRepositoryUniqueId(iheXdsFileInfo.getUniqueId());
        DataHandler dataHandler = null;
        try {
        	String fileString="http://127.0.0.1:8080/2024/XDS/Repository/"+
                    iheXdsFileInfo.getUniqueId()+"."+DhccFileUtils.getFileType(iheXdsFileInfo.getMimeType());
//        	String fileString=iheXdsFileInfo.getFileName();
                    
        	log.info(fileString);
            dataHandler = new DataHandler(new URL(fileString));
        } catch (MalformedURLException e) {
            log.info(e.getMessage(), e);
        }
        retrievedDocument1.setDataHandler(dataHandler);

		return retrievedDocument1 ;

	}
	public List<IheXdsFileInfo> find(String id) throws SQLException {
		XdsDto xdsDto = new XdsDto();
		List<IheXdsFileInfo> iheXdsInfoFileList = new ArrayList<IheXdsFileInfo>();
		IheXdsFileInfo iheXdsFileInfo =new IheXdsFileInfo();
		iheXdsFileInfo.setUniqueId(id);
		iheXdsInfoFileList.add(iheXdsFileInfo);
		xdsDto.setIheXdsInfoList(iheXdsInfoFileList);
		DataDao dataDao = new DataDao();
		return dataDao.findFileData(xdsDto);
	}
}
